/*
 * $Id: PpadsCommandUtil.java,v 1.3 2009/07/10 16:27:06 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.util;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.exceptions.BasicException;
import com.ge.healthcare.autosc.common.exceptions.PpadsCommandUtilException;

public class PpadsCommandUtil {

	public static final String UNREGISTERED_SITE = "unregisteredSite";
	public static final String FILL = "fill";
	public static final String TOUCH = "touch";
	public static final String LIMIT_ALARM = "limitAlarm";
	public static final String DISCRETE_ALARM = "discreteAlarm";
	public static final String IRI = "iri";

	/**
	 * Post a PPADS event. This calls the postPpadsEvent script, setting up the
	 * appropriate arguments, waits for it to complete, and checks the return
	 * value.
	 * 
	 * @param functionName
	 *            The name of the event function.
	 * 
	 * @param iriData
	 *            The IRI data string. This is null if no IRI data is specified.
	 * 
	 * @exception AscServiceAbortException
	 *                If an error is detected executing the script.
	 */
	public static void postPpadsEvent(String systemId, String functionName,
			String taskId) throws PpadsCommandUtilException {
		String methodName = "postPpadsEvent";
		StringBuffer buffer = new StringBuffer();
		String perlScript = ApplicationInfo.getInstance().getProperty(
				PropertyConstants.POST_PPADS_EVENT,
				"/ascpdata3/data2/ppads/bin/postPpadsEvent");
		buffer.append(perlScript);
		buffer.append(" -s ");
		buffer.append(systemId);
		buffer.append(" -f ");
		buffer.append(functionName);
		buffer.append(" -r ");
		buffer.append(taskId);
		String command = buffer.toString();
		ASCLogger.info(PpadsCommandUtil.class, methodName,
				"Execute postPpadsEvent command: " + command, true);
		String[] envList = buildPpadsEnvList();
		runPostPpadsEvent(command, envList);
	}

	/**
	 * Post a PPADS event. This calls the postPpadsEvent script, setting up the
	 * appropriate arguments, waits for it to complete, and checks the return
	 * value.
	 * 
	 * @param functionName
	 *            The name of the event function.
	 * 
	 * @param iriData
	 *            The IRI data string. This is null if no IRI data is specified.
	 * 
	 * @exception AscServiceAbortException
	 *                If an error is detected executing the script.
	 */
	public static void postPpadsEvent(String systemId, String functionName,
			String taskId, String iriData) throws PpadsCommandUtilException {
		String methodName = "postPpadsEvent";
		StringBuffer buffer = new StringBuffer();
		String perlScript = ApplicationInfo.getInstance().getProperty(
				PropertyConstants.POST_PPADS_EVENT,
				"/ascpdata3/data2/ppads/bin/postPpadsEvent");
		buffer.append(perlScript);
		buffer.append(" -s ");
		buffer.append(systemId);
		buffer.append(" -f ");
		buffer.append(functionName);
		buffer.append(" -r ");
		buffer.append(taskId);
		if (iriData != null) {
			buffer.append(" -i ");
			buffer.append(iriData);
		}
		String command = buffer.toString();
		ASCLogger.info(PpadsCommandUtil.class, methodName,
				"Execute postPpadsEvent command: " + command, true);
		String[] envList = buildPpadsEnvList();
		runPostPpadsEvent(command, envList);
	}

	private static String[] buildPpadsEnvList() {
		String[] envList = null;
		String ppadsEnv = ApplicationInfo.getInstance().getProperty(
				PropertyConstants.PPADS_ENVIRONMENTS);
		if (!ASCUtil.isNullOrEmpty(ppadsEnv)) {
			envList = ppadsEnv.split(";");
		}
		return envList;
	}

	/**
	 * Run the postPpadsEvent Perl command and checks its results. The command
	 * is considered to have failed if an exception is thrown running the
	 * command, the exit value is non-zero, anything was written to standard
	 * output, or anything was written to standard error.
	 * 
	 * @param command
	 *            The Perl command to run. The command will be parsed into
	 *            tokens using white-space as delimiters.
	 * 
	 * @param envList
	 *            Environment variables to apply to the command. Each element in
	 *            the array is an environment variable in the form name=value.
	 *            If there there are no environment variables to define, use an
	 *            empty string array.
	 * 
	 * @exception AscServiceAbortException
	 *                If the command failed, post a fatal error using the
	 *                results of the command as the detailed error message.
	 */
	private static void runPostPpadsEvent(String command, String[] envList)
			throws PpadsCommandUtilException {
		ExecResult cmdResult = null;
		try {
			cmdResult = new ExecResult(command, envList);
		} catch (BasicException be) {
			throw new PpadsCommandUtilException(be);
		}
		if ((cmdResult.getExitValue() != 0)
				|| (cmdResult.getStdOutput().length != 0)
				|| (cmdResult.getStdError().length != 0)) {
			throw new PpadsCommandUtilException(cmdResult.toString());
		}
	}
}
